package com.wlhse.controller;

import com.wlhse.cache.JedisClient;
import com.wlhse.dto.*;
import com.wlhse.dto.getDto.StaffIDGetDto;
import com.wlhse.dto.inDto.CheckRecordInDto;
import com.wlhse.dto.inDto.SupervisionScopeInDto;
import com.wlhse.entity.SuperVisionAreaCompany;
import com.wlhse.service.SupervisionTaskService2;
import com.wlhse.util.state_code.NR;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController("ProblemRecordController")
@RequestMapping("/api/v1")
public class ProblemRecordController {

    @Resource
    private JedisClient jedisClient;

    @Resource
    private SupervisionTaskService2 supervisionTaskService2;

    @RequestMapping(value = "/task_time",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getTaskTime(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userId = jedisClient.get(token);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date=df.format(new Date());
        return supervisionTaskService2.querryTaskTime(userId,date);
    }

    @RequestMapping(value = "/check_level",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckLevel(){
        return supervisionTaskService2.querryCheckLevel();
    }

    @RequestMapping(value = "/login_name",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getLoginName(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userId = jedisClient.get(token);
        return supervisionTaskService2.querryLoginName(userId);
    }

    @RequestMapping(value = "/check_list",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckList(HttpServletRequest request){
        return supervisionTaskService2.querryCheckList();
    }

    @RequestMapping(value = "/check_project/{id}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckProject(HttpServletRequest request,@PathVariable String id){
        return supervisionTaskService2.querryCheckProject(id);
    }

    @RequestMapping(value = "/check_Item",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckItem(@ModelAttribute CheckDto checkDto){
        return supervisionTaskService2.querryCheckItem(checkDto.getProjectCode(),checkDto.getListID());
    }

    @RequestMapping(value = "/problem_type",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getProblemType(@ModelAttribute CheckDto checkDto){
        return supervisionTaskService2.querryProblemType(checkDto.getProjectCode(),checkDto.getListID());
    }

    @RequestMapping(value = "/problem_rank",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getProblemRank(@ModelAttribute CheckDto checkDto){
        return supervisionTaskService2.querryProblemRank(checkDto.getProjectCode(),checkDto.getListID());
    }

    @RequestMapping(value = "/problem_source",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getProblemSourcr(){
        return supervisionTaskService2.querryProblemSourcr();
    }

    @RequestMapping(value = "/problem_gist",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getProblemGist(@ModelAttribute CheckDto checkDto){
        return supervisionTaskService2.querryProblemGist(checkDto.getProjectCode(),checkDto.getListID());
    }

    @RequestMapping(value = "/task_detail2",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getTaskDetail(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userId = jedisClient.get(token);
        return supervisionTaskService2.querryTaskDetail(userId);
    }

    @RequestMapping(value = "/stafftask_detail",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getStaffTask(@ModelAttribute SupervisionTaskDto supervisionTaskDto){
        return supervisionTaskService2.querryStaffTask(supervisionTaskDto);
    }

    @RequestMapping(value = "/task_content/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getTaskContent(@PathVariable String code){
        return supervisionTaskService2.querryTaskContent(code);
    }

    @RequestMapping(value = "/check_table/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckTable(@PathVariable String code){
        return supervisionTaskService2.querryCheckTable(code);
    }

    @RequestMapping(value = "/addCheckTimes",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String addCheckTimes(@ModelAttribute SupervisionTaskDto supervisionTaskDto){
        return supervisionTaskService2.addActuaInspectionTimes(supervisionTaskDto.getSupervisionTaskCode(), supervisionTaskDto.getTaskItem());
    }

    @RequestMapping(value = "/querryHazardFactor/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getHazardFactory(@PathVariable String code){
        return supervisionTaskService2.querryHazardFactors(code);
    }

    @RequestMapping(value = "/queryUserMessage",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getUserMessage2(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userID=jedisClient.get(token);
        return supervisionTaskService2.queryUserMessgaer(userID);
    }

    @RequestMapping(value = "/addUserMessage",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String addUserMessage2(@RequestBody(required = false) InMessage2 inMessage2,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userID=jedisClient.get(token);
        inMessage2.setForm_user_id(userID);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date=df.format(new Date());
        inMessage2.setMessage_date(date);
        inMessage2.setMessage_status("未读");
        return supervisionTaskService2.addUserMessager(inMessage2);
    }

    @RequestMapping(value = "/addCause",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String addCause(){
        return supervisionTaskService2.addCause();
    }

    @RequestMapping(value = "/querySubComEmployee/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String querySubComEmployee(@PathVariable String code){
        return supervisionTaskService2.querySubComEmployee(code);
    }

    @RequestMapping(value = "/querySubComUserEmployee/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String querySubComUserEmployee(@PathVariable String code){
        return supervisionTaskService2.querySubComUserEmployee(code);
    }

    @RequestMapping(value = "/queryManageEmployee",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryManageEmployee(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userID=jedisClient.get(token);
        return supervisionTaskService2.queryManageEmployee(userID);
    }

    @RequestMapping(value = "/querySubComEmployeeByType/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String querySubComEmployeeByType(@PathVariable String code,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userID=jedisClient.get(token);
        return supervisionTaskService2.querySubComEmployeeByType(code,userID);
    }

    //查询当前登录人的checkrecord
    @RequestMapping(value = "/queryCheckRecordByID",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryCheckRecordByID(@ModelAttribute CheckRecordInDto checkRecordInDto, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String userID=jedisClient.get(token);
        return supervisionTaskService2.queryCheckRecordByID(checkRecordInDto,userID);
    }

    //修改checkrecord内容，并把status设为录入,判断是否增加检查次数
    @RequestMapping(value = "/updateCheckRecordByRecordID",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateCheckRecordByRecordID(@RequestBody CheckRecordDto checkRecordDto){
        return supervisionTaskService2.updateCheckRecordByRecordID(checkRecordDto);
    }

    //根据id删除checkrecord
    @RequestMapping(value = "/deleteCheckRecordByID/{id}",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
    public String deleteCheckRecordByID(@PathVariable String id){
        return supervisionTaskService2.deleteCheckRecordByID(id);
    }

    //删除并修改检查区域
    @RequestMapping(value = "/updateSupervisionScope",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String updateSupervisionScope(@RequestBody(required = false) SupervisionScopeInDto supervisionScopeInDtos){
        return supervisionTaskService2.updateSupervisionScope(supervisionScopeInDtos);
    }

    //查询角色为2008的所有员工
    @RequestMapping(value = "/queryUserByRoleCode/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryUserByRoleCode(@PathVariable String code){
        return supervisionTaskService2.queryUserByRoleCode(code);
    }

    //查询检查区域
    @RequestMapping(value = "/queryAreaName",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryAreaName(){
        return supervisionTaskService2.queryAreaName();
    }

    //根据checkListCode查询完整的检查表
    @RequestMapping(value = "/completeCheckTable/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryCompleteCheckTable(@PathVariable String code){
        return supervisionTaskService2.queryCompleteCheckTable(code);
    }

    //查询某个人还未分配的检查区域
    @RequestMapping(value = "/queryUntreatedSupervisionArea",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryUntreatedSupervisionArea(@ModelAttribute SupervisionAreaDto supervisionAreaDto){
        return supervisionTaskService2.queryUntreatedSupervisionArea(supervisionAreaDto.getAreaID()
                ,supervisionAreaDto.getCompanyType(),supervisionAreaDto.getStaffID());
    }

    //查询某个人还未分配的检查类型
    @RequestMapping(value = "/queryUntreatedCheckType",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryUntreatedCheckType(@ModelAttribute SupervisionAreaDto supervisionAreaDto){
        return supervisionTaskService2.queryUntreatedCheckType(supervisionAreaDto.getCode(),supervisionAreaDto.getStaffID());
    }

    //查询某个人还未分配的检查类型
    @RequestMapping(value = "/querySupervisionAreaCompanyByArea",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String querySupervisionAreaCompanyByArea(@ModelAttribute SuperVisionAreaCompany superVisionAreaCompany){
        return supervisionTaskService2.querySupervisionAreaCompanyByArea(superVisionAreaCompany.getAreaName(),superVisionAreaCompany.getSupervisionAreaID());
    }

    //查询未分配监督任务的公司
    @RequestMapping(value = "/queryUntreatedCompanies",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryUntreatedCompanies(@ModelAttribute SuperVisionAreaCompany superVisionAreaCompany){
        String name=superVisionAreaCompany.getAreaName();
        String code=superVisionAreaCompany.getSupervisionAreaID();
        if (name==null){
            name="";
        }else if (code==null){
            code="";
        }
        return supervisionTaskService2.queryUntreatedCompanies(name,code);
    }

    //修改某区域的检查公司
    @RequestMapping(value = "/updateSuperVisionAreaCompany",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateSuperVisionAreaCompany(@RequestBody(required = false) SuperVisionAreaCompanyDto superVisionAreaCompanyDto){
        return supervisionTaskService2.updateSuperVisionAreaCompany(superVisionAreaCompanyDto.getSupervisionAreaID(),superVisionAreaCompanyDto.getSuperVisionAreaCompanies());
    }

    //查询检查类型
    @RequestMapping(value = "/queryCheckType",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryCheckType(){
        return supervisionTaskService2.queryCheckType();
    }

    //查询检查轮次
    @RequestMapping(value = "/queryCheckRound",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryCheckRound(){
        return supervisionTaskService2.queryCheckRound();
    }

    //问题严重性统计
    @RequestMapping(value = "/countProblemRank",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String countProblemRank(@ModelAttribute CountProblemDto countProblemDto){
        return supervisionTaskService2.countProblemRank(countProblemDto);
    }

    //模糊查询返回值
    @RequestMapping(value = "/check_table_find",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String getCheckTableBySearch(@RequestParam("code") String code,@RequestParam("value") String value){
        return supervisionTaskService2.querryCheckTableBySearch(code,value);
    }
}
